<?php
/**
 * 公休节假日管理
 * @author a
 *
 */
class Holiday extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->load->model('holiday_time_model');
//         $this->load->model('teacher_work_time_model');
        
        //分页
//         $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
//         $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
//         $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    public function Index(){
        try {
        	
        	$holiday_time_list = $this->holiday_time_model->findAll();
        	
        	
        	//echo '<pre>';print_r($holiday_time_list);die();
        	
        	$this->t->write_view('content', 'admin/holiday/index', array(
        			'holiday_time_list'	=>	$holiday_time_list,
        	), false);
        	$this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'nursery/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/nursery/teacher/',
            ), false);
            $this->t->render();
        }
    }
    public function add(){
    	try {
    		$y = $this->input->post('y');if(empty($y))throw new Exception('月为空');
    		if($y<10)$y = "0$y";
    		$r = $this->input->post('r');if(empty($r))throw new Exception('日为空');
    		if($r<10)$r = "0$r";
    		$id = $this->holiday_time_model->find(array('holiday'=>$y.'-'.$r),'id');
    		if(!empty($id))throw new Exception('日期已存在');
    		
    		$bool = $this->holiday_time_model->insert(array('holiday'=>$y.'-'.$r,'create_date'=>date('Y-m-d H:i:s')));
    		if(empty($bool))throw new Exception('创建失败');
    		
    		redirect("/admin/holiday");
    	}catch (Exception $e){
    		$error = $e->getMessage();
    		$this->t->write_view('content', 'nursery/msg_error', array(
    				'error'     =>  $error,
    				'time'  =>  10,
    				//'url'   =>  '/nursery/teacher/',
    		), false);
    		$this->t->render();
    	}
    }
    /**
     * 获取时间
     */
    public function gettime(){
    	try {
    		$return = array('code'=>10,'msg'=>'OK');
    		$y = $this->input->get('y');if(empty($y))throw new Exception('月为空');
    		if($y<10)$y = "0$y";
    		$r = $this->input->get('r');if(empty($r))throw new Exception('日为空');
    		if($r<10)$r = "0$r";
    		
    		$holiday = $this->holiday_time_model->find(array('holiday'=>$y.'-'.$r),'id');
    		if(!empty($holiday))throw new Exception('日期已存在');
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);exit();
    }
    /**
     * 修改时间ajax
     */
    public function Updatetimeajax(){
    	try {
    		$return = array('code'=>10,'msg'=>'OK','id'=>0);
    		
    		$checked = $this->input->get('checked');
    		
    		$id = intval($this->input->get('id'));
    		if(empty($id))throw new Exception('id为空');
    		$data = array();
    		$data['status'] = $checked == 'true' ? 1 : 0;
    		$data['update_date'] = date('Y-m-d H:i:s');
    		$bool = $this->holiday_time_model->update($data,array('id'=>$id));
    		if(empty($bool))throw new Exception('失败');
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);exit();
    }
    
    
    
    
    
    
    
    
    
    
}






























